﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.IRepository;
using DAL.Admin;
using Entities;
using BLL.BLL;

namespace WindowsForms.Admin
{
    public partial class FormGiaiDau : Form
    {
        int nums = 5;
        int page = 1;
        int tong;
        bool state = true;
        public IGiaiDauRepository gd = new BLTGiaiDauRepository();
        public FormGiaiDau()
        {
            InitializeComponent();
            datagrgiaidau.AutoGenerateColumns = false;
        }

        private void btrefresh_Click(object sender, EventArgs e)
        {
            tong = ManagerForm.TongTrang(gd.LayTatCa().Count, nums);
            LoadGiaiDau(getPage(page));
            tbdl.Text = page + " / " + tong;
        }

        private void FormGiaiDau_Load(object sender, EventArgs e)
        {


        }

        private void datagrgiaidau_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {


                int id;
                id = Convert.ToInt32(datagrgiaidau.Rows[e.RowIndex].Cells[1].Value);
                GiaiDau giaidau = gd.LayTheoId(id);
                if (e.ColumnIndex == 4)
                {

                    gd.XoaGiaiDau(giaidau);
                    btrefresh.PerformClick();

                }
                if (e.ColumnIndex == 5)
                {

                    getControl(giaidau);
                }
            }
            catch
            {


            }
        }

        private void getControl(GiaiDau giaidau)
        {

            if (giaidau != null)
                mkmagiaidau.ReadOnly = true;
            mkmagiaidau.Text = giaidau.MaGiaiDau + "";
            tbtengd.Text = giaidau.TenGiaiDau;
            dtpngaykhoito.Value = giaidau.ThoiGianBatDau;
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            GiaiDau gd1 = new GiaiDau();

            gd1.TenGiaiDau = tbtengd.Text;
            gd1.ThoiGianBatDau = dtpngaykhoito.Value;
            if (!QuanLiGiaiDau.KiemTra(mkmagiaidau.Text, gd.LayTatCa()))
            {
                gd.ThemGiaiDau(gd1);
                btrefresh.PerformClick();
            }
            else
            {
                gd1.MaGiaiDau = Convert.ToInt32(mkmagiaidau.Text);
                DialogResult rs = MessageBox.Show("Giải Đấu đã có bạn muốn cập nhật không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rs == DialogResult.Yes)
                {
                    gd.CapNhapGiaiDau(gd1);
                    btrefresh.PerformClick();


                }
            }

        }

        private void btnrefe_Click(object sender, EventArgs e)
        {
            mkmagiaidau.Text = "";
            tbtengd.Text = "";
            dtpngaykhoito.Value = DateTime.Now;
        }

        private void FormGiaiDau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                btrefresh.PerformClick();
            if (e.KeyCode == Keys.Escape)
                btnthoat.PerformClick();
            if (e.Control && e.KeyCode == Keys.F)
                tsbtim.PerformClick();
        }

        private void tsbtim_Click(object sender, EventArgs e)
        {
            FormTimGiaiDau frtim = new FormTimGiaiDau();
            frtim.FormClosed += new FormClosedEventHandler(frtim_FormClosed);
            frtim.ShowDialog();
        }

        void frtim_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (FormTimGiaiDau.state)
            {

                string data = FormTimGiaiDau.data;
                List<GiaiDau> ds = gd.LayTenGiaiDau(data);
                if (ds.Count > 0)
                    LoadGiaiDau(ds);
            }

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttien_Click(object sender, EventArgs e)
        {

            if (page < tong)
                page++;

            if (page <= tong)
            {
                tbdl.Text = page + " / " + tong;
                LoadGiaiDau(getPage(page));

            }
        }

        private void btlui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                tbdl.Text = page + " / " + tong;
                LoadGiaiDau(getPage(page));
            }
        }
        public List<GiaiDau> getPage(int pages)
        {
            var query = from c in gd.LayTatCa()
                           .Skip(nums * pages - nums).Take(nums)
                        select c;
            return query.ToList();


        }
        public void LoadGiaiDau(List<GiaiDau> ds)
        {
            datagrgiaidau.DataSource = ds;
            for (int i = 0; i < datagrgiaidau.Rows.Count; i++)
                datagrgiaidau.Rows[i].Cells[0].Value = i + 1;

        }

        private void tbdl_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(tbdl.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadGiaiDau(getPage(t));
                        tbdl.Text = t + " / " + tong;
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btsort_Click(object sender, EventArgs e)
        {
            List<GiaiDau> kq;
            if (state)
            {
                kq = QuanLiGiaiDau.SapXepTang(getPage(page));
                state = !state;

            }
            else
            {
                kq = QuanLiGiaiDau.SapXepGiam(getPage(page));
                state = !state;
            }
            LoadGiaiDau(kq);

        }

       





    }
}
